﻿namespace IPMS.BLL
{
    using Resources;
    using System;
    using System.Collections.Generic;

    public static class IPDataFormat
    {
        public static string ConvertDateFormat(DateTime date, int Mode)
        {
            return ConvertDateFormat(date.Month.ToString() + "/" + date.Day.ToString() + "/" + date.Year.ToString(), Mode);
        }

        public static string ConvertDateFormat(string date, int Mode)
        {
            string ret;
            string strMonth = "";
            string strDay = "";
            string strYear = "";
            string strDate = "";
            int DayIndex = 0;
            int MonthIndex = 0;
            try
            {
                switch (Mode)
                {
                    case 1:
                        DayIndex = date.IndexOf("/", 0);
                        strDay = date.Substring(0, DayIndex);
                        MonthIndex = date.IndexOf("/", (int) (DayIndex + 1));
                        strMonth = date.Substring(DayIndex + 1, (MonthIndex - DayIndex) - 1);
                        strYear = date.Substring(MonthIndex + 1, 4);
                        if (strMonth.Length != 2)
                        {
                            strMonth = "0" + strMonth;
                        }
                        if (strDay.Length != 2)
                        {
                            strDay = "0" + strDay;
                        }
                        strDate = strMonth + "/" + strDay + "/" + strYear;
                        break;

                    case 2:
                        MonthIndex = date.IndexOf("/", 0);
                        strMonth = date.Substring(0, MonthIndex);
                        DayIndex = date.IndexOf("/", (int) (MonthIndex + 1));
                        strDay = date.Substring(MonthIndex + 1, (DayIndex - MonthIndex) - 1);
                        strYear = date.Substring(DayIndex + 1, 4);

                        if (strMonth.Length != 2)
                        {
                            strMonth = "0" + strMonth;
                        }
                        if (strDay.Length != 2)
                        {
                            strDay = "0" + strDay;
                        }
                        strDate = strDay + "/" + strMonth + "/" + strYear;
                        break;
                }
                try
                {
                    if (Mode == 1)
                    {
                        DateTime oDate = Convert.ToDateTime(strDate);
                    }
                    return strDate;
                }
                catch (Exception)
                {
                    return "";
                }
            }
            catch (Exception)
            {
                ret = "";
            }
            return ret;
        }

        public static string ConvertDateFormat(int day, int month, int year)
        {
            string strMonth = "";
            string strDay = "";
            string strYear = "";
            try
            {
                strMonth = month.ToString();
                strDay = day.ToString();
                strYear = year.ToString();
                if (strMonth.Length != 2)
                {
                    strMonth = "0" + strMonth;
                }
                if (strDay.Length != 2)
                {
                    strDay = "0" + strDay;
                }
                return (strDay + "/" + strMonth + "/" + strYear);
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string DateFormat(string date)
        {
            return Convert.ToDateTime(date).ToShortDateString();
        }

        public static string ErrorMessage(string strValue)
        {
            if (strValue.IndexOf("PK_") != 0)
            {
                return Messages.mCannotInsertDuplicate.ToString();
            }
            return strValue;
        }

        public static string FormatDateTime(DateTime dt)
        {
            if (dt == Convert.ToDateTime("01/01/1753"))
            {
                return "";
            }
            return ConvertDateFormat(dt.Day, dt.Month, dt.Year);
        }

        public static string FormatDateTime(string dt)
        {
            string ret;
            try
            {
                dt = ConvertDateFormat(dt, 1);
                try
                {
                    if (Convert.ToDateTime(dt) == Convert.ToDateTime("01/01/1753"))
                    {
                        return "";
                    }
                    ret = ConvertDateFormat(string.Concat(new object[] { Convert.ToDateTime(dt).Month.ToString(), '/', Convert.ToDateTime(dt).Day.ToString(), '/', Convert.ToDateTime(dt).Year.ToString() }), 2);
                }
                catch (Exception)
                {
                    ret = string.Empty;
                }
            }
            catch
            {
                dt = ConvertDateFormat(dt, 1);
                try
                {
                    if (Convert.ToDateTime(dt) == Convert.ToDateTime("01/01/1753"))
                    {
                        return "";
                    }
                    ret = Convert.ToDateTime(dt).ToShortDateString();
                }
                catch (Exception)
                {
                    ret = string.Empty;
                }
            }
            return ret;
        }

        public static string GenerateMessage(Exception ex, List<string> values)
        {
            string strMessage = "";
            try
            {
                //if (string.IsNullOrEmpty(ex.InnerException.Message))
                //{
                //    strMessage = ex.Message.ToString();
                //}
                //else
                //{
                //    strMessage = ex.InnerException.Message.ToString();
                //}
                if (ex.InnerException == null)
                {
                    strMessage = ex.Message.ToString();
                }
                else
                {
                    strMessage = ex.InnerException.Message.ToString();
                }
            }
            catch (Exception)
            {
                strMessage = ex.Message.ToString();
            }
            return GenerateMessage(strMessage, values);
        }

        public static string GenerateMessage(string msg, List<string> values)
        {
            int index = 1;
            if (msg.IndexOf("PK_") >= 0)
            {
                msg = Messages.mCannotInsertDuplicate.ToString();
                return msg;
            }
            string StringMsg = Messages.ResourceManager.GetString(msg);
            if (StringMsg != "")
            {
                msg = StringMsg;
            }
            try
            {
                while (index <= values.Count)
                {
                    msg = msg.Replace("%" + index.ToString(), values[index - 1]);
                    index++;
                }
            }
            catch (Exception)
            {
            }
            return msg;
        }

        public static DateTime GetValidDateTime(string dt)
        {
            try
            {
                if (dt == "")
                {
                    return DateTime.MinValue;
                }
                return Convert.ToDateTime(dt);
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        public static string ValidString(string strValue)
        {
            if (string.IsNullOrEmpty(strValue))
            {
                return "";
            }
            return strValue;
        }
    }
}

